﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HodhodNews.Extraction;
using Googler.DAL;
using HodhodNews;
using Googler.BLL;

namespace Googler.Web
{

    public partial class Full : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int id = Convert.ToInt32(Request.QueryString["ID"]);
                ExtractAndView(id, ExtractionAlgorithm.CommonParent, false);
            }
        }

        private void ExtractAndView(int id, ExtractionAlgorithm algorithm, bool overridePreviousExtraction)
        {
            lblResult.Visible = true;
            if (id < 1)
            {
                return;
            }

            var context = new DataContext();
            var news = context.News.Where(n => n.OId == id).FirstOrDefault();
            if (news == null)
            {
                return;
            }
            hlArtilceUrl.NavigateUrl = news.Link;
            hlArtilceUrl.Text = news.Link;

            //if (news.IsExtracted != null)
            //{
            //    btnFaild.Enabled = !Convert.ToBoolean(news.IsExtracted);
            //    btnSuccess.Enabled = Convert.ToBoolean(news.IsExtracted);
            //}
            if (Convert.ToBoolean(news.IsExtracted) == false || overridePreviousExtraction)
            {
                var result = NewsExtractionProvider.GetProvider(algorithm).Extract(new ExtracationOptions()
                 {
                     NewsUrl = news.Link,
                     NewsSummaryText = news.Summary,
                     NewsTitle = ""
                 });

                if (result != null)
                {
                    lblResult.Text = result.Text;
                    news.Text = result.Text;
                    news.IsExtracted = true;
                    news.LanguageID = (int)result.Language;
                    context.SaveChanges();
                    if (result.Language == HodhodNews.Language.Arabic)
                    {
                        divContainer.Style["direction"] = "rtl";
                    }
                    else
                    {
                        divContainer.Style["direction"] = "ltr";
                    }
                }
            }
            else
            {
                lblResult.Text = news.Text;
            }
        }

        protected void btnFaild_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(Request.QueryString["ID"]);
            var context = new DataContext();
            var news = context.News.Where(n => n.OId == id).FirstOrDefault();
            if (news == null)
            {
                return;
            }
            var url = context.TestedUrls.Where(u => u.Url == news.Link).FirstOrDefault();
            if (url != null)
            {
                url.FaildToExtract = true;
                context.SaveChanges();
                return;
            }
            else
            {
                url = new TestedUrl();
                url.Url = news.Link;
                context.AddToTestedUrls(url);
                context.SaveChanges();
            }
            news.IsExtracted = false;
            context.SaveChanges();
        }

        protected void btnSuccess_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(Request.QueryString["ID"]);
            var context = new DataContext();
            var news = context.News.Where(n => n.OId == id).FirstOrDefault();
            if (news == null)
            {
                return;
            }
            news.IsExtracted = true;
            context.SaveChanges();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(Request.QueryString["ID"]);
            var context = new DataContext();
            var news = context.News.Where(n => n.OId == id).FirstOrDefault();
            if (news == null)
            {
                return;
            }
            news.IsDeleted = true;
            context.SaveChanges();

            var list = context.NewsKeywords.Where(n => n.NewsID == id).ToList();
            list.ForEach(nk => nk.IsNewsDeleted = true);
            context.SaveChanges();
            Response.Redirect("~");
        }

        protected void btnReextract_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(Request.QueryString["ID"]);
            ExtractAndView(id, ExtractionAlgorithm.Boilerpipe, true);
        }
    }
}